package com.mooco.dongba.common.web;

import com.mooco.dongba.common.pojo.JsonResult;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandleRuntimeException(RuntimeException e) {
        return new JsonResult(e);
    }

    @ExceptionHandler(ShiroException.class)
    public JsonResult doHandleShiroException(ShiroException e) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(0);
        if (e instanceof UnknownAccountException) {
            jsonResult.setMessage("账户不存在");
        } else if (e instanceof LockedAccountException) {
            jsonResult.setMessage("账户已禁用");
        } else if (e instanceof IncorrectCredentialsException) {
            jsonResult.setMessage("密码不正确");
        } else if (e instanceof AuthorizationException) {
            jsonResult.setMessage("没有此操作权限");
        } else {
            jsonResult.setMessage("系统维护中");
        }
        return jsonResult;
    }
}
